import { Component, OnInit, Injector, ViewChild, Output, EventEmitter } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { LazyLoadEvent } from 'primeng/api';
import { Paginator } from 'primeng/primeng';
import { Table } from 'primeng/table';
import { IBasicModelsInfo } from './base-models-info';
import { finalize } from 'rxjs/operators';
import { CodeItemsTreeComponent } from '@app/shared/common/codeitemtree/codeitems-tree.component';
import { CodeRulesNumComponent } from './code-nrules.component';
import { CodeItemServiceProxy, CodeItemTreeEditDto, CodeItemTreeImportDto } from '@shared/service-proxies/stock-service-proxies';

interface SelectedItems {
    name: string;
    id: string;
}
@Component({
    templateUrl: './code-items.component.html'
})
export class CodeItemsComponent extends AppComponentBase implements OnInit {
    constructor(injector: Injector, private _codeItemsServiceProxy: CodeItemServiceProxy) {
        super(injector);
    }
    @ViewChild('codeRulesNumModal') codeRulesNumModal: CodeRulesNumComponent;
    @ViewChild('ouCodeItemsTree') ouCodeItemsTree: CodeItemsTreeComponent;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    codeitem: CodeItemTreeEditDto = new CodeItemTreeEditDto();

    filterText = '';
    saving = false;
    basicModelsInfo: IBasicModelsInfo;
    f_TimeCode;
    f_SequenceCode;
    selectDate; //日期格式下拉框
    // selectNum:Array<object>=[]; //流水号下拉框
    // nums:SelectedItems;
    f_IsSeparators;
    ouSelected(event) { }
    ngOnInit(): void {
        this._codeItemsServiceProxy.getRuleForCombobox('time', '').subscribe(result => {
            this.f_TimeCode = result.items.map(e => {
                return {
                    label: e.displayText,
                    value: e
                };
            });
            this.selectDate = result.items;
        });
        // this._codeItemsServiceProxy.getRuleForCombobox('num', "").subscribe(result => {
        //     this.selectNum = result.items.map(e => {
        //         return {
        //             name: e.displayText,
        //             id: e.value
        //         };
        //     });
        //     // this.selectNum = result.items;
        // });
    }
    // searchTextInputClick(): void {
    //     // this._codeItemsServiceProxy.getRuleForCombobox('num', value).subscribe(result => {
    //     //     this.f_SequenceCode = result.items.map(e => {
    //     //         return {
    //     //             label: e.displayText,
    //     //             value: e
    //     //         };
    //     //     });
    //     //     this.selectNum = result.items;
    //     // });
    // }
    onUpload(event) {
        if (this.codeitem.id == null || this.codeitem.f_ParCodeItemId) {
            this.notify.error('请选择根目录进行导入')
            return
        }
        // let list = event as Array<any>;
        let list = event.fileData[0].sheetJson;
        let customerImportIntoList = list.map(el => {
            return {
                level1Code: el.一级分类,
                level1Name: el.__EMPTY,
                level2Code: el.二级分类,
                level2Name: el.__EMPTY_1,
                level3Code: el.三级分类,
                level3Name: el.__EMPTY_2,
                remark: el.备注
            };
        }) as Array<CodeItemTreeImportDto>;
        console.log(customerImportIntoList)
        this.message.confirm(
            '确认导入这' + customerImportIntoList.length + '条数据吗？',
            this.l('AreYouSure'),
            async (isConfirmed) => {
                if (isConfirmed) {
                    abp.ui.setBusy(undefined);
                    this._codeItemsServiceProxy.importCodeItemTree(customerImportIntoList, this.codeitem.id).pipe(finalize(() => {
                        abp.ui.clearBusy()
                    })).subscribe(() => {
                        this.notify.success('导入成功')
                    })
                }
            }
        );
    }
    rowSelect(event): void {
        this.f_SequenceCode = event.f_RuleName;
        this.codeitem.f_SequenceCode = event.id;
    }
    ouallSelected(event: any): void {
        this.basicModelsInfo = event;
        this._codeItemsServiceProxy.getCodeItemsForEdit(event.Id).subscribe(result => {
            this.codeitem = result;
            if (this.codeitem.f_IsUsing == 'false') {
                this.codeitem.f_IsUsing = null;
            }
            if (this.codeitem.f_IsSeparator == 'false') {
                this.codeitem.f_IsSeparator = null;
            }
            if (this.codeitem.f_IsSequence == 'false') {
                this.codeitem.f_IsSequence = null;
            }
            if (this.codeitem.f_IsTime == 'false') {
                this.codeitem.f_IsTime = null;
            }
            if (this.codeitem.f_IsCodeItem == 'false') {
                this.codeitem.f_IsCodeItem = null;
            }
            this.f_SequenceCode = result.f_SequenceCodeName;
        });
    }

    //新增编码
    createCodeItems(): void {
        this.codeitem = new CodeItemTreeEditDto();
        this.codeitem.f_ParCodeItemId = this.basicModelsInfo.Id;
        // this.ouCodeItemsModal.show(this.basicModelsInfo);
    }
    //新增编码根目录
    createGCodeItems(): void {
        this.codeitem = new CodeItemTreeEditDto();
    }
    //保存编码
    save(): void {
        // this._codeItemsServiceProxy
        // .createOrUpdateCodeItemTreeAsync(this.codeitem)
        // .pipe(
        //     finalize(() => {
        //         this.saving = false;
        //     })
        // )
        // .subscribe(() => {
        //     this.notify.info(this.l('SavedSuccessfully'));
        // });
        this.saving = true;
        let re;
        this._codeItemsServiceProxy
            .getCodeItemId(this.codeitem.id, this.codeitem.f_CodeItemId)
            .pipe(
                finalize(() => {
                    if (re == 'true') {
                        this.message.warn('特征码不允许重复');
                        this.saving = false;
                        return;
                    } else {
                        this._codeItemsServiceProxy
                            .createOrUpdateCodeItemTreeAsync(this.codeitem)
                            .pipe(
                                finalize(() => {
                                    this.saving = false;
                                })
                            )
                            .subscribe(() => {
                                this.notify.info(this.l('SavedSuccessfully'));
                                this.ouCodeItemsTree.getTreeDataFromServer();
                            });
                    }
                })
            )
            .subscribe(result => {
                if (!result) {
                    re = 'false';
                } else {
                    re = 'true';
                }
            });
    }
    //删除编码
    deleteCodeItems(id): void {
        let self = this;
        self.message.confirm(self.l('提示', id), this.l('确定删除？'), isConfirmed => {
            if (isConfirmed) {
                this._codeItemsServiceProxy.deleteCodeItemTreeAsync(id).subscribe(result => {
                    abp.notify.success(this.l(result));
                });
            }
        });
    }
}
